package com.example.CarManagement.sys.controller;


import com.alibaba.fastjson2.JSON;
import com.example.CarManagement.common.Result;
import com.example.CarManagement.sys.entity.Permission;
import com.example.CarManagement.sys.entity.Role;
import com.example.CarManagement.sys.entity.RolePermission;
import com.example.CarManagement.sys.mapper.RoleMapper;
import com.example.CarManagement.sys.mapper.UserRoleMapper;
import com.example.CarManagement.sys.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色管理 前端控制器
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-08
 */

@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Resource
    private RoleServiceImpl roleService;


    // 获取角色列表
    @PreAuthorize("@se.hasPermi('ROLE_SELECT')")
    @GetMapping("/getRoleList")
    public Result<?> getRoleList(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo) {

        return roleService.getRoleByPage(pageSize, pageNo);
    }


    // 用户设置权限 获取所有的权限id与名称
    @PreAuthorize("@se.hasPermi('USER_SELECT')")
    @GetMapping("/userSelectList")
    public Result<?> list() {
        return roleService.getRoleByUserSelectList();
    }

    @PreAuthorize("@se.hasPermi('ROLE_UPDATE')")
    @GetMapping("/{id}")
    public Result<?> getRoleById(@PathVariable String id) {
        return roleService.getRoleById(id);
    }

    // 修改某个角色权限
    @PreAuthorize("@se.hasPermi('ROLE_UPDATE')")
    @PostMapping("/update")
    public Result<?> updateRole(@RequestBody Map<String, Object> data) {

        Role role = new Role();

        List<String> rolePermissionIds = null;
        try {
            Map<String, Object> roleMap = (Map<String, Object>) data.get("role");
            role.setId((String) roleMap.get("id"));
            role.setName((String) roleMap.get("name"));
            role.setRemark((String) roleMap.get("remark"));
            role.setDefault((Boolean) roleMap.get("default"));
            role.setDeleted((Boolean) roleMap.get("deleted"));

            Object permissionListIds = data.get("permissionList");
            rolePermissionIds = JSON.parseArray(JSON.toJSONString(permissionListIds), String.class);

        } catch (Exception e) {

            return Result.fail("参数错误");
        }
        if (rolePermissionIds != null) {
            if (rolePermissionIds.size() < 1) {
                return Result.fail("请选择权限");
            }
            return roleService.updateRole(role, rolePermissionIds);
        } else {
            return Result.fail("参数错误");
        }

    }

    // 添加角色
    @PreAuthorize("@se.hasPermi('ROLE_INSERT')")
    @PostMapping("/add")
    public Result<?> addRole(@RequestBody Map<String, Object> data) {
        Role role = new Role();

        List<String> rolePermissionIds = null;
        try {
            Map<String, Object> roleMap = (Map<String, Object>) data.get("role");
            role.setId((String) roleMap.get("id"));
            role.setName((String) roleMap.get("name"));
            role.setRemark((String) roleMap.get("remark"));
            role.setDeleted((Boolean) roleMap.get("deleted"));
            role.setDefault((Boolean) roleMap.get("default"));
            Object permissionListIds = data.get("permissionList");
            rolePermissionIds = JSON.parseArray(JSON.toJSONString(permissionListIds), String.class);
            
        } catch (Exception e) {

            return Result.fail("参数错误");
        }
        if (rolePermissionIds != null) {
            if (rolePermissionIds.size() < 1) {
                return Result.fail("请选择权限");
            }
            return roleService.addRole(role, rolePermissionIds);
        } else {
            return Result.fail("参数错误");
        }

    }

    // 删除角色
    @PreAuthorize("@se.hasPermi('ROLE_DELETE')")
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteRole(@PathVariable String id) {
        return roleService.deleteRole(id);
    }

}
